#coding=utf=8
from django.shortcuts import render, redirect, get_object_or_404, render_to_response
from .models import *
from .forms import *
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth import authenticate, login, logout
from django.http import JsonResponse
from django.views.decorators.cache import cache_page
from django.template import RequestContext
import markdown2, urlparse
from django.db.models import Q
#分页用
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):  
    article_list = Article.object.query_by_time()
    context = {'article_list': article_list}

    # 公共请求
    common_list = common()
    context_list = dict(context.items()+common_list.items())
    return render_to_response('article_page.html',RequestContext(request,context_list))

#分页
def test(request):
    article_list = Article.object.query_by_time()
    context = RequestContext(request,{'latest_article_list': article_list })
    return render_to_response('test.html',context)

#文章详细
def article(request, article_id):
    article_list = Article.object.filter(id=article_id)
    context = {'article_list': article_list}
    # 公共请求
    common_list = common()
    context_list = dict(context.items()+common_list.items())
    return render_to_response('article_page.html',RequestContext(request,context_list))

#文章标签 点标签时，到这里
def article_tag_page(request, tag):
    tag_list = Article.object.filter( Q(tag1=tag) | Q(tag2=tag) | Q(tag3=tag) )
    context = {'article_list': tag_list}
    # 公共请求
    common_list = common()
    context_list = dict(context.items()+common_list.items())
    return render_to_response('article_page.html',RequestContext(request,context_list))

# 公共请求
def common():
    #右边 热门段子精选 10个
    article_list_10 = Article.object.query_by_polls()[0:10]
    #右边 图片10个
    Imgs_list_10 = Img.object.query_by_polls()[0:12]
    
    common_list = {'Pulls':article_list_10, 'Imgs':Imgs_list_10}
    return common_list



#动图
def gif_page(request):
    gif_list = Img.object.query_by_time()
    context = {'gif_list': gif_list}
    # print gif_list
    # 公共请求
    common_list = common()
    context_list = dict(context.items()+common_list.items())
    return render(request, 'gif_page.html', RequestContext(request,context_list))

#动图详细
def gif_id(request, gif_id):
    gif_list = Img.object.filter(id=gif_id)
    context = {'gif_list': gif_list}
    # print gif_list
    # 公共请求
    common_list = common()
    context_list = dict(context.items()+common_list.items())
    return render(request, 'gif_page.html', RequestContext(request,context_list))

#动图标签 点标签时，到这里
def gif_tag_page(request, tag):   
    tag_list = Img.object.filter( Q(tag1=tag) | Q(tag2=tag) | Q(tag3=tag) )
    context = {'gif_list': tag_list}
    # 公共请求
    common_list = common()
    context_list = dict(context.items()+common_list.items())
    return render(request, 'gif_page.html', RequestContext(request,context_list))




@login_required
def comment(request, article_id):
    form  = CommmentForm(request.POST)
    url = urlparse.urljoin('/focus/', article_id)
    if form.is_valid():
        user = request.user
        article = Article.object.get(id=article_id)
        new_comment = form.cleaned_data['comment']
        c = Comment(content=new_comment, article_id=article_id)  # have tested by shell
        c.user = user
        c.save()
        article.comment_num += 1
    return redirect(url)

@login_required
def get_keep(request, article_id):
    logged_user = request.user
    article = Article.object.get(id=article_id)
    articles = logged_user.article_set.all()
    if article not in articles:
        article.user.add(logged_user)  # for m2m linking, have tested by shell
        article.keep_num += 1
        article.save()

        return redirect('/focus/')
    else:
        url = urlparse.urljoin('/focus/', article_id)
        return redirect(url)

@login_required
def get_poll_article(request,article_id):
    logged_user = request.user
    article = Article.object.get(id=article_id)

    #polls = logged_user.poll_set.all()
    polls = logged_user
    articles = []
    for poll in polls:
        articles.append(poll.article)

    if article in articles:
        url = urlparse.urljoin('/focus/', article_id)
        return redirect(url)
    else:
        article.poll_num += 1
        article.save()
        poll = Poll(user=logged_user, article=article)
        poll.save()
        data = {}
        return redirect('/focus/')











